# -*- coding: utf-8 -*-
# created on 2016/12/20
from sympy import plot, Interval
from mathsolver.functions.base import BaseFunction
from mathsolver.functions.sympy_utils import default_symbol
from mathsolver.functions.hanshu.helper import check_eq


# 作函数图像
class HanShuZuoTu(BaseFunction):
    """作出函数图像"""

    def solver(self, *args):
        # 输入为 BaseEq 或者 BaseFunc
        eq = check_eq(args[0])

        y, expr = eq.sympify()
        symbol = default_symbol(expr)

        # optional 输入为图像区间
        if len(args) == 2:
            qujian = args[1].interval
            assert isinstance(qujian, Interval)
        else:
            qujian = None

        try:
            hashnum = hash(str(expr))
            if qujian:
                plot(expr, (symbol, qujian.args[0], qujian.args[1]), show=False).save("data/%d.png" % hashnum)
            else:
                plot(expr, show=False).save("data/%d.png" % hashnum)
            graph = "static/images/%d.png" % hashnum
            self.steps.append(["%s的图像为" % self.output_eq([y, expr]), graph])
        except Exception:
            pass

        self.label.add("作函数图像")
        return self


if __name__ == '__main__':
    pass
